package com.efast.dmom.equipment.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.portal.entity.common.PortalAttachmentForm;
import com.efast.cafe.portal.remote.common.PortalAttachmentService;
import com.efast.dmom.equipment.api.EquipCheckingControllerApi;
import com.efast.dmom.equipment.bean.CheckingTaskParameterBean;
import com.efast.dmom.equipment.bean.EquipCheckingProgramOrder;
import com.efast.dmom.equipment.bean.EquipCheckingTaskBean;
import com.efast.dmom.equipment.bean.EquipCheckingTaskDetailsBean;
import com.efast.dmom.equipment.bean.EquipIdsParamBean;
import com.efast.dmom.equipment.entity.EquipCheckingTaskProgramItem;
import com.efast.dmom.equipment.service.checking.IEquipCheckingOrderService;

/**
 * 
 * @author 作者 Aaron Liu:
 * 
 * @version 创建时间：2019年8月13日 下午4:29:26
 * 
 *          类说明
 * 
 */
@Controller
@RequestMapping("/equipment")
public class EquipCheckingController implements EquipCheckingControllerApi {

	@Autowired
	private IEquipCheckingOrderService equipCheckingService;

	@Autowired
	private PortalAttachmentService portalAttachmentService;

	@Override
	@RequestMapping(value = "/checking/deleteCheckingRules", method = RequestMethod.POST)
	public @ResponseBody void deleteCheckingRules(@RequestBody EquipIdsParamBean bean) {

		equipCheckingService.deleteCheckingRules(bean.getIds());
	}

	@Override
	@RequestMapping(value = "/checking/deleteCheckingOrders/{checkId}", method = RequestMethod.POST)
	public @ResponseBody void deleteCheckingOrders(@PathVariable Integer checkId) {
		equipCheckingService.deleteCheckingOrders(checkId);
	}

	@Override
	@RequestMapping(value = "/checking/saveCheckProgram", method = RequestMethod.POST)
	public @ResponseBody void addCheckingProgramOrder(@RequestBody EquipCheckingProgramOrder bean) {
		if (bean.getId() != null) {
			equipCheckingService.updateCheckingProgramOrder(bean);
		} else {
			equipCheckingService.addCheckingProgramOrder(bean);
		}

	}

	@Override
	@RequestMapping(value = "/checking/deleteCheckProgram", method = RequestMethod.POST)
	public @ResponseBody void deleteCheckingProgramOrder(@RequestBody EquipCheckingProgramOrder bean) {

		equipCheckingService.deleteCheckingProgramOrder(bean.getId());

	}

	@Override
	@RequestMapping(value = "/checking/getCheckingTasks", method = RequestMethod.POST)
	public @ResponseBody EquipCheckingTaskBean getCheckingTasks(@RequestBody EquipCheckingTaskBean taskBean) {

		return equipCheckingService.queryTaskList(taskBean);
	}

	@Override
	@RequestMapping(value = "/checking/queryTaskDetails", method = RequestMethod.POST)
	public @ResponseBody List<EquipCheckingTaskDetailsBean> getCheckingTaskDetails(
			@RequestBody CheckingTaskParameterBean taskIdBean) {
		return equipCheckingService.getCheckingTaskDetails(taskIdBean.getTaskId());
	}

	@Override
	@RequestMapping(value = "/checking/startCheckingTask", method = RequestMethod.POST)
	public @ResponseBody void startCheckingTask(@RequestBody CheckingTaskParameterBean taskIdBean) {
		equipCheckingService.updateCheckingTaskStatus(taskIdBean.getTaskId(), "started");

	}

	@Override
	@RequestMapping(value = "/checking/saveCheckingTask", method = RequestMethod.POST)
	public @ResponseBody void saveCheckingTask(@RequestBody EquipCheckingTaskDetailsBean taskDetailsBean) {

		equipCheckingService.saveCheckingTask(taskDetailsBean);

		List<PortalAttachmentForm> attachments = taskDetailsBean.getAttachments();

		portalAttachmentService.saveAttachmentForm(taskDetailsBean.getGenerateFormId(), attachments);

	}

	@Override
	@RequestMapping(value = "/checking/finishCheckingTask", method = RequestMethod.POST)
	public @ResponseBody void finishCheckingTask(@RequestBody EquipCheckingTaskDetailsBean taskDetailsBean) {

		// equipCheckingService.saveCheckingTask(taskDetailsBean);
		equipCheckingService.updateCheckingTaskStatus(taskDetailsBean.getTaskId(), "finished");
	}

	@Override
	@RequestMapping(value = "/checking/getCheckingProgramDetails", method = RequestMethod.POST)
	public @ResponseBody List<EquipCheckingTaskProgramItem> getCheckingProgramDetails(
			@RequestBody EquipCheckingTaskDetailsBean taskDetailsBean) {

		return equipCheckingService.getCheckingProgramDetails(taskDetailsBean.getProgramId());
	}

}
